import 'package:flutter/material.dart';
import 'package:flutter_gallery/data/gallery_options.dart';
import 'package:flutter_gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:intl/intl.dart';

class DialogDemo extends StatefulWidget {
  const DialogDemo({Key? key, required this.type}) : super(key: key);

  final DialogDemoType type;

  @override
  _DialogDemoState createState() => _DialogDemoState();
}

class _DialogDemoState extends State<DialogDemo> with RestorationMixin {
  late RestorableRouteFuture<String> _alertDialogRoute;
  late RestorableRouteFuture<String> _alertDialogWithTitleRoute;
  late RestorableRouteFuture<String> _simpleDialogRoute;

  @override
  String? get restorationId => 'dialog_demo';

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(_alertDialogRoute, 'alert_demo_dialog_route');
    registerForRestoration(
        _alertDialogWithTitleRoute, 'alert_demo_with_title_dialog_route');
    registerForRestoration(_simpleDialogRoute, 'simple_dialog_route');
  }

  @override
  void initState() {
    super.initState();
    _alertDialogRoute = RestorableRouteFuture<String>(
        onPresent: (navigator, arguments) =>
            navigator.restorablePush(_alertDialogDemoRoute),
        onComplete: _showInSnackBar);
    _alertDialogWithTitleRoute = RestorableRouteFuture<String>(
        onPresent: (navigator, arguments) =>
            navigator.restorablePush(_alertDialogWithTitleDemoRoute),
        onComplete: _showInSnackBar);
    _simpleDialogRoute = RestorableRouteFuture(
        onPresent: (navigator, arguments) =>
            navigator.restorablePush(_simpleDialogDemoRoute),
        onComplete: _showInSnackBar);
  }

  @override
  void dispose() {}

  // Displays the popped String value in a snackbar.
  void _showInSnackBar(String value) {
    // The value passed to Navigator.pop() or null.
    if (value != null) {
      ScaffoldMessenger.of(context).hideCurrentSnackBar();
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text(
            GalleryLocalizations.of(context)!.dialogSelectedOption(value),
          ),
        ),
      );
    }
  }

  static Route<String> _alertDialogDemoRoute(
      BuildContext context, Object? arguments) {
    final theme = Theme.of(context);
    final dialogTextStyle = theme.textTheme.subtitle1!
        .copyWith(color: theme.textTheme.caption!.color);
    return DialogRoute<String>(
      context: context,
      builder: (context) => ApplyTextOptions(
          child: AlertDialog(
        content: Text(
          GalleryLocalizations.of(context)!.dialogDiscardTitle,
          style: dialogTextStyle,
        ),
        actions: [
          _DialogButton(text: GalleryLocalizations.of(context)!.dialogCancel),
          _DialogButton(text: GalleryLocalizations.of(context)!.dialogDiscard),
        ],
      )),
    );
  }

  static Route<String> _simpleDialogDemoRoute(
      BuildContext context, Object? arguments) {
    final theme = Theme.of(context);

    return DialogRoute(
      context: context,
      builder: (context) => ApplyTextOptions(
        child: SimpleDialog(
          title: Text(GalleryLocalizations.of(context)!.dialogLocationTitle),
          children: [
            _DialogDemoItem(
              icon: Icons.account_circle,
              color: theme.colorScheme.primary,
              text: 'username@gmail.com',
            ),
            _DialogDemoItem(
              icon: Icons.account_circle,
              color: theme.colorScheme.secondary,
              text: 'user02@gmail.com',
            ),
            _DialogDemoItem(
              icon: Icons.add_circle,
              color: theme.disabledColor,
              text: GalleryLocalizations.of(context)!.dialogAddAccount,
            ),
          ],
        ),
      ),
    );
  }

  static Route<String> _alertDialogWithTitleDemoRoute(
      BuildContext context, Object? argument) {
    final theme = Theme.of(context);
    final dialogTextStyle = theme.textTheme.subtitle1!
        .copyWith(color: theme.textTheme.caption!.color);

    return DialogRoute(
      context: context,
      builder: (context) => ApplyTextOptions(
        child: AlertDialog(
          title: Text(GalleryLocalizations.of(context)!.dialogLocationTitle),
          content: Text(
            GalleryLocalizations.of(context)!.dialogLocationDescription,
            style: dialogTextStyle,
          ),
          actions: [
            _DialogButton(
                text: GalleryLocalizations.of(context)!.dialogDisagree),
            _DialogButton(text: GalleryLocalizations.of(context)!.dialogAgree)
          ],
        ),
      ),
    );
  }

  static Route<void> _fullscreenDialogRoute(
      BuildContext context, Object? arguments) {
    return MaterialPageRoute(
      builder: (context) => const _FullScreenDialogDemo(),
      fullscreenDialog: true,
    );
  }

  String _title(BuildContext context) {
    switch (widget.type) {
      case DialogDemoType.alert:
        return GalleryLocalizations.of(context)!.demoAlertDialogTitle;
      case DialogDemoType.alertTitle:
        return GalleryLocalizations.of(context)!.demoAlertTitleDialogTitle;
      case DialogDemoType.simple:
        return GalleryLocalizations.of(context)!.demoSimpleDialogTitle;
      case DialogDemoType.fullscreen:
        return GalleryLocalizations.of(context)!.demoFullscreenDialogTitle;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Navigator(
      // Adding [ValueKey] to make sure that the widget gets rebuilt when
      // changing type.
      key: ValueKey(widget.type),
      restorationScopeId: 'navigator',
      onGenerateRoute: (settings) {
        return _NoAnimationMaterialPageRoute(
          settings: settings,
          builder: (context) => Scaffold(
            appBar: AppBar(
              automaticallyImplyLeading: false,
              title: Text(_title(context)),
            ),
            body: Center(
              child: ElevatedButton(
                child: Text(GalleryLocalizations.of(context)!.dialogShow),
                onPressed: () {
                  switch (widget.type) {
                    case DialogDemoType.alert:
                      _alertDialogRoute.present();
                      break;
                    case DialogDemoType.alertTitle:
                      _alertDialogWithTitleRoute.present();
                      break;
                    case DialogDemoType.simple:
                      _simpleDialogRoute.present();
                      break;
                    case DialogDemoType.fullscreen:
                      Navigator.restorablePush<void>(
                          context, _fullscreenDialogRoute);
                      break;
                  }
                },
              ),
            ),
          ),
        );
      },
    );
  }
}

// _NoAnimationMaterialPageRoute
class _NoAnimationMaterialPageRoute<T> extends MaterialPageRoute<T> {
  _NoAnimationMaterialPageRoute(
      {required WidgetBuilder builder,
      RouteSettings? settings,
      bool maintainState = true,
      bool fullscreenDialog = false})
      : super(
          builder: builder,
          maintainState: maintainState,
          settings: settings,
          fullscreenDialog: fullscreenDialog,
        );

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return child;
  }
}

// _DialogButton
class _DialogButton extends StatelessWidget {
  const _DialogButton({Key? key, required this.text}) : super(key: key);

  final String text;

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: () {
          Navigator.of(context).pop(text);
        },
        child: Text(text));
  }
}

// _DialogDemoItem
class _DialogDemoItem extends StatelessWidget {
  const _DialogDemoItem(
      {Key? key, required this.icon, required this.color, required this.text})
      : super(key: key);

  final IconData icon;
  final Color color;
  final String text;

  @override
  Widget build(BuildContext context) {
    return SimpleDialogOption(
      onPressed: () {
        Navigator.of(context).pop(text);
      },
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(icon, size: 36, color: color),
          Flexible(
            child: Padding(
              padding: const EdgeInsetsDirectional.only(start: 16),
              child: Text(text),
            ),
          ),
        ],
      ),
    );
  }
}

// _FullScreenDialogDemo
class _FullScreenDialogDemo extends StatelessWidget {
  const _FullScreenDialogDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    // Remove the MediaQuery padding because the demo is rendered inside of a
    // different page that already accounts for this padding.
    return MediaQuery.removePadding(
        context: context,
        child: ApplyTextOptions(
          child: Scaffold(
            appBar: AppBar(
              title:
                  Text(GalleryLocalizations.of(context)!.dialogFullscreenTitle),
              actions: [
                TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text(
                    GalleryLocalizations.of(context)!.dialogFullscreenSave,
                    style: theme.textTheme.bodyText2!.copyWith(
                      color: theme.colorScheme.onPrimary,
                    ),
                  ),
                ),
              ],
            ),
            body: Center(
              child: Text(
                GalleryLocalizations.of(context)!.dialogFullscreenDescription,
              ),
            ),
          ),
        ));
  }
}
